home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SDIOLE.PAK / SDIOLE.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  274 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1994 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl/pch.h>
  5. #include <owl/applicat.h>
  6. #include <owl/editfile.h>
  7. #include <owl/mdi.h>
  8. #include <owl/decmdifr.h>
  9. #include <owl/statusba.h>
  10. #include <owl/controlb.h>
  11. #include <owl/buttonga.h>
  12. #include <owl/gdiobjec.h>
  13. #include <owl/ocfevent.h>
  14. #include <owl/uihelper.h>
  15. #include <owl/oleframe.h>
  16.  
  17. #include <ocf/pch.h>
  18. #include <ocf/ocapp.h>
  19. #include <ocf/ocdoc.h>
  20. #include <ocf/ocview.h>
  21. #include <ocf/ocstorag.h>
  22. #include <ocf/ocreg.h>
  23.  
  24. #include "sampcont.h"
  25. #include "sdiole.h"
  26.  
  27. // Locations for toolbar placement
  28. //
  29. const TDecoratedFrame::TLocation Location[] = {
  30.   TDecoratedFrame::Top,
  31.   TDecoratedFrame::Left,
  32.   TDecoratedFrame::Right
  33. };
  34.  
  35. // App registration object
  36. //
  37. static TPointer<TOcRegistrar> Registrar;
  38.  
  39. // Registration tables
  40. //
  41. REGISTRATION_FORMAT_BUFFER(100)
  42.  
  43. BEGIN_REGISTRATION(AppReg)
  44.  REGDATA(clsid,      "{5E4BD410-8ABC-101B-A23B-CE4E85D07ED2}")
  45.  REGDATA(appname,    "SdiOle")
  46. END_REGISTRATION
  47.  
  48. //----------------------------------------------------------------------------
  49. // Ole SDI application class
  50. //
  51.  
  52. class TOwlSdiApp : public TApplication, public TOcModule {
  53.   public:
  54.     TOwlSdiApp();
  55.    ~TOwlSdiApp();
  56.     void InitMainWindow();
  57.     void InitInstance();
  58.  
  59.   protected:
  60.     TOpenSaveDialog::TData   FileData;
  61.     TGadgetWindow::THintMode HintMode;
  62.     TOleFrame*               Frame;
  63.     TControlBar*             ControlBar;
  64.     int                      ControlBarLoc;
  65.  
  66.   protected:
  67.     void CmFileNew();
  68.     void CmFileOpen();
  69.     void CmToggleHint();
  70.     void CeToggleHint(TCommandEnabler&);
  71.     void CmToggleBar();
  72.     void CmToggleStatus();
  73.     void CeToggleStatus(TCommandEnabler&);
  74.  
  75.   DECLARE_RESPONSE_TABLE(TOwlSdiApp);
  76. };
  77.  
  78. DEFINE_RESPONSE_TABLE1(TOwlSdiApp, TApplication)
  79.   EV_COMMAND(CM_FILENEW, CmFileNew),
  80.   EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  81.   EV_COMMAND(CM_TOGGLEHINT, CmToggleHint),
  82.   EV_COMMAND_ENABLE(CM_TOGGLEHINT, CeToggleHint),
  83.   EV_COMMAND(CM_TOGGLEBAR, CmToggleBar),
  84.   EV_COMMAND(CM_TOGGLESTATUS, CmToggleStatus),
  85.   EV_COMMAND_ENABLE(CM_TOGGLESTATUS, CeToggleStatus),
  86. END_RESPONSE_TABLE;
  87.  
  88.  
  89. TOwlSdiApp::TOwlSdiApp()
  90.  : TApplication(::AppReg["appname"])
  91. {
  92.   // Make OC app helper
  93.   //
  94.   OcInit(*::Registrar, ::Registrar->GetOptions());
  95. }
  96.  
  97. TOwlSdiApp::~TOwlSdiApp()
  98. {
  99. }
  100.  
  101. static TControlBar*
  102. BuildControlBar(TWindow* parent, TControlBar::TTileDirection direction)
  103. {
  104.   TControlBar* cb = new TControlBar(parent, direction);
  105.   cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW));
  106.   cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN));
  107.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE));
  108.   cb->Insert(*new TButtonGadget(CM_FILESAVEAS, CM_FILESAVEAS));
  109.  
  110.   cb->Insert(*new TSeparatorGadget(6));
  111.   cb->Insert(*new TButtonGadget(CM_EDITUNDO, CM_EDITUNDO));
  112.   cb->Insert(*new TButtonGadget(CM_EDITCUT, CM_EDITCUT));
  113.   cb->Insert(*new TButtonGadget(CM_EDITCOPY, CM_EDITCOPY));
  114.   cb->Insert(*new TButtonGadget(CM_EDITPASTE, CM_EDITPASTE));
  115.  
  116.   cb->Insert(*new TSeparatorGadget(6));
  117.   cb->Insert(*new TButtonGadget(CM_EDITFIND, CM_EDITFIND));
  118.   cb->Insert(*new TButtonGadget(CM_EDITREPLACE, CM_EDITREPLACE));
  119.   cb->Insert(*new TButtonGadget(CM_EDITFINDNEXT, CM_EDITFINDNEXT));
  120.  
  121.   cb->Insert(*new TSeparatorGadget(6));
  122.   cb->Insert(*new TButtonGadget(CM_TOGGLEHINT, CM_TOGGLEHINT,
  123.                       TButtonGadget::NonExclusive));
  124.   cb->Insert(*new TSeparatorGadget(6));
  125.   cb->Insert(*new TButtonGadget(CM_TOGGLEBAR, CM_TOGGLEBAR));
  126.  
  127.   cb->Attr.Style |= WS_CLIPSIBLINGS;    // since toolbar may move around
  128.   cb->Attr.Id = IDW_TOOLBAR;
  129.  
  130.   return cb;
  131. }
  132.  
  133. //
  134. // Construct the TOwlSdiApp's MainWindow loading its menu, accelerator table
  135. // & icon
  136. //
  137. void
  138. TOwlSdiApp::InitMainWindow()
  139. {
  140.   Frame = new TOleFrame(GetName(), 0, true);
  141.   SetMainWindow(Frame);
  142.  
  143.   Frame->Attr.AccelTable = IDA_SDIFILE;
  144.   Frame->SetMenuDescr(TMenuDescr(IDM_EDITFILE_DOC));
  145.   Frame->SetIcon(this, IDI_SINGLEFILE);
  146.  
  147.   // Construct, build and insert a control bar into the frame. Start out at
  148.   // the top of the frame
  149.   //
  150.   HintMode = TGadgetWindow::PressHints;
  151.   ControlBarLoc = 0;
  152.   ControlBar = BuildControlBar(Frame, TControlBar::Horizontal);
  153.   Frame->Insert(*ControlBar, Location[ControlBarLoc]);
  154.  
  155.   // Construct a status bar & insert it at the bottom
  156.   //
  157.   TStatusBar* sb = new TStatusBar(0, TGadget::Recessed,
  158.            TStatusBar::CapsLock | TStatusBar::NumLock | TStatusBar::Overtype);
  159.  
  160.   Frame->Insert(*sb, TDecoratedFrame::Bottom);
  161.  
  162.   EnableCtl3d(true);
  163.  
  164.   // Initialize the file data struct used for open and saveas
  165.   //
  166.   FileData.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
  167.   FileData.SetFilter(string(*this, IDS_DOCFILEFILTER).c_str());
  168. }
  169.  
  170. //
  171. // Call file new command handler to get the initial new window
  172. //
  173. void
  174. TOwlSdiApp::InitInstance()
  175. {
  176.   TApplication::InitInstance();
  177.   CmFileNew();
  178. }
  179.  
  180. //
  181. // Respond to "New" command by constructing, creating, and setting up a
  182. // new TFileWindow MDI child
  183. //
  184. void
  185. TOwlSdiApp::CmFileNew()
  186. {
  187.   // check to save old...
  188.  
  189.   TWindow* client = new TOleSampContainer(0, 0);
  190.   TWindow* oldClient = GetMainWindow()->SetClientWindow(client);
  191.   if (oldClient) {
  192.     oldClient->Destroy();
  193.     delete oldClient;
  194.   }
  195. }
  196.  
  197. //
  198. // Respond to "Open" command by constructing, creating, and setting up a
  199. // new TFileWindow, and deleting the old one
  200. //
  201. void
  202. TOwlSdiApp::CmFileOpen()
  203. {
  204.   // check to save old...
  205.  
  206.   *FileData.FileName = 0;
  207.   if (TFileOpenDialog(MainWindow, FileData).Execute() == IDOK) {
  208.     TWindow* client = 0;
  209.  
  210.     TRY {
  211.       client = new TOleSampContainer(0, FileData.FileName);
  212.     }
  213.     CATCH ((TXObjComp x) {
  214.       GetMainWindow()->MessageBox(x.why().c_str(), GetName(), 
  215.         MB_OK|MB_ICONEXCLAMATION);
  216.       return;
  217.     }
  218.  
  219.     TWindow* oldClient = GetMainWindow()->SetClientWindow(client);
  220.     if (oldClient) {
  221.       oldClient->Destroy();
  222.       delete oldClient;
  223.     }
  224.   }
  225. }
  226.  
  227. void
  228. TOwlSdiApp::CeToggleHint(TCommandEnabler& ce)
  229. {
  230.   ce.SetCheck(HintMode == TGadgetWindow::EnterHints);
  231. }
  232.  
  233. void
  234. TOwlSdiApp::CmToggleHint()
  235. {
  236.   HintMode = HintMode==TGadgetWindow::PressHints ?
  237.                TGadgetWindow::EnterHints :
  238.                TGadgetWindow::PressHints;
  239.  
  240.   ControlBar->SetHintMode(HintMode);
  241.   ControlBar->SetHintCommand(-1);  // make sure we don't leave hint text dangling
  242. }
  243.  
  244. void
  245. TOwlSdiApp::CmToggleBar()
  246. {
  247.   ControlBarLoc = (ControlBarLoc+1) % COUNTOF(Location);
  248.   ControlBar->SetDirection(ControlBarLoc==0 ?
  249.                            TControlBar::Horizontal : TControlBar::Vertical);
  250.   Frame->Insert(*ControlBar, Location[ControlBarLoc]);
  251.   Frame->Layout();
  252. }
  253.  
  254. void
  255. TOwlSdiApp::CeToggleStatus(TCommandEnabler& ce)
  256. {
  257.   ce.SetCheck(GetMainWindow()->ChildWithId(IDW_STATUSBAR)->IsWindowVisible());
  258. }
  259.  
  260. void
  261. TOwlSdiApp::CmToggleStatus()
  262. {
  263.   GetMainWindow()->SendMessage(WM_COMMAND, IDW_STATUSBAR);  // toggle status bar on/off
  264. }
  265.  
  266. int
  267. OwlMain(int /*argc*/, char* /*argv*/ [])
  268. {
  269.   ::Registrar = new TOcRegistrar(::AppReg, 0, TApplication::GetCmdLine());
  270.   if (::Registrar->IsOptionSet(amAnyRegOption))
  271.     return 0;
  272.   return TOwlSdiApp().Run();
  273. }
  274.